import Vue from "vue";
import VueRouter from "vue-router";
import vuex from '../store/index';
import HomeView from "../views/HomeView.vue";

Vue.use(VueRouter);

const routes = [{
		path: "/",
		name: "login",
		redirect: "/LoginView",
		//component: HomeView,
	},
	{
		path: "/LoginView",
		name: "LoginView",
		component: () =>
			import("../views/LoginView.vue"),
	},
	{
		path: "/HomeView",
		name: "HomeView",
		component: HomeView,
		meta: {
			requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
		},
	},
	{
		path: "/cateView",
		name: "cateView",
		component: () =>
			import("../views/cateView.vue"),
		meta: {
			requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
		},
	},
	{
		path: "/articleView",
		name: "articleView",
		component: () =>
			import("../views/articleView.vue"),
		meta: {
			requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
		},
	},
	{
		path: "/blogarticleView",
		name: "blogarticleView",
		component: () =>
			import("../views/blogarticleView.vue"),
	},
	{
		path: "/AboutView",
		name: "AboutView",
		component: () =>
			import("../views/AboutView.vue"),
		meta: {
			requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
		},
	},
	{
		path: "/editor",
		name: "editor",
		component: () =>
			import("../components/editor.vue"),
		meta: {
			requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
		},
	},
	{
		path: "/privacyPolicy",
		name: "privacyPolicy",
		component: () =>
			import("../views/privacy_policy.vue"),
		meta: {
			requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
		},
	},
	{
		path: "/content",
		name: "content",
		component: () =>
			import("../components/content.vue"),
		meta: {
			requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
		},
	}
];

const router = new VueRouter({
	routes,
});

router.beforeEach((to, from, next) => {
	// console.log(to) // 即将要进入的目标路由对象
	// console.log(from) // 当前导航正要离开的路由
	// next()// 回调函数
	// let userinfo = localStorage.userinfo;
	let userinfo = Storage.prototype.getCanExpireLocal("userinfo");
	// console.log(userinfo);
	if (to.meta.requireAuth) { //进行判断跳到哪个路径的值，如果为false的话不必进行路由守卫/则为true的话进行判断
		if (userinfo == '值已失效' || userinfo == null) {
			next("/LoginView");
			vuex.commit('changeuserinfo', JSON.parse(userinfo))
		} else {
			next();
			vuex.commit('changeuserinfo', JSON.parse(userinfo))
		}
	} else {
		next()
	}
})

export default router;
